class Solution {
    public:
        int getMaxLen(vector<int>& nums) {
            int n = nums.size();
            vector<int> f(n);//以i位置为结尾的乘积为正数的最长子数组长度
            vector<int> g(n);//以i位置为结尾的乘积为负数的最长子数组长度
            f[0] = nums[0] > 0 ? 1 : 0;
            g[0] = nums[0] < 0 ? 1 : 0;
            //当nums[0] == 0 时f[0]and g[0] is 0;
            int ret = f[0];
            for(int i = 1 ; i < n ; i++){
                if(nums[i] > 0){
                    f[i] = f[i-1]+1;
                    g[i] = g[i-1] == 0 ? 0 : g[i-1]+1;
                }else if(nums[i] < 0){
                    f[i] =g[i-1]==0 ? 0: g[i-1]+1;
                    g[i] = f[i-1]+1;
                }else{
                    f[i] = g[i] = 0;
                }
                ret =max(ret,f[i]);
            }
            return ret;
        }
    };